package com.chujian.Activities;

import java.text.SimpleDateFormat;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.chujian.Adapter.OrderHistoryDetailConsumerAdapter;
import com.chujian.Adapter.OrderHistoryDetailSellerAdapter;
import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.model.FoodList;
import com.chujian.model.Order;
import com.chujian.orderingsystem.R;

public class OrderHistoryDetailConsumerActivity extends Activity{
	private TextView mOrderId;
	private TextView mTableName;
	private TextView mOrderTime;
	private TextView mUserPhone;
	private TextView mTotalPrice;
	private List<FoodList> mList;
	private Order mOrder;
	private ListView mListView;
	private int deletePosition;
	private OrderHistoryDetailConsumerAdapter mAdapter;
	private Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			if(str.equals("deleteSuccess")){
				Toast.makeText(OrderHistoryDetailConsumerActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
				Intent data=new Intent();
				data.putExtra("deletePosition", deletePosition);
				setResult(1001, data);
				OrderHistoryDetailConsumerActivity.this.finish();
			}else if(str.equals("deleteFailed")){
				Toast.makeText(OrderHistoryDetailConsumerActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
			}else if(str!=null&&!str.equals("")){
				mList=JsonUtil.FoodListFromJson(str);
				mAdapter=new OrderHistoryDetailConsumerAdapter(mList, OrderHistoryDetailConsumerActivity.this);
				mListView.setAdapter(mAdapter);
			}
				
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_history_detail_consumer);
		initView();
		
		Intent intent=this.getIntent();
		String jsonString=intent.getStringExtra("mOrder");
		deletePosition=intent.getIntExtra("position", -1);
//		Toast.makeText(this, jsonString, Toast.LENGTH_LONG).show();
		mOrder=JsonUtil.OrderFromJson(jsonString);
		Log.i("info", mOrder.toString());
		if(mOrder!=null){
//			mList=mOrder.getFoodList();
			
			SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			long millis=Long.parseLong(mOrder.getOrderTime());
			Log.i("info", millis+"");
			String dateTime = dateFm.format(new java.util.Date(millis));
			
			mOrderId.setText(mOrder.getOrderId());
			mTableName.setText(mOrder.getTableName());
			mOrderTime.setText(dateTime);
			mUserPhone.setText(mOrder.getUserPhone());
			mTotalPrice.setText(mOrder.getTotalPrice()+"元");
//			Toast.makeText(this, mList.toString(), Toast.LENGTH_LONG).show();
//			if(mList!=null&&mList.size()>0){
//				mAdapter=new OrderHistoryDetailConsumerAdapter(mList, OrderHistoryDetailConsumerActivity.this);
//				mListView.setAdapter(mAdapter);
//			}
			
			String url=Constan.BASE_URL+"OrderCurrentDetailSellerFoodList";
			new HttpClientPostThread(url, mHandler, mOrder.getOrderId()).start();
			
			
		}
		
		
	}
	private void initView() {
		// TODO Auto-generated method stub
		mOrderId=(TextView) findViewById(R.id.tv_order_current_list_item_id);
		mTableName=(TextView) findViewById(R.id.tv_order_current_list_item_table);
		mOrderTime=(TextView) findViewById(R.id.tv_order_current_list_item_time);
		mUserPhone=(TextView) findViewById(R.id.tv_order_current_list_item_phone);
		mTotalPrice=(TextView) findViewById(R.id.tv_order_current_list_item_price);
		mListView=(ListView) findViewById(R.id.lv_order_history_detail_consumer);
		
	}
	public void deleteOrder(View v) {
		// 删除订单按钮
		confirmDialog();
	}
	private void confirmDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(OrderHistoryDetailConsumerActivity.this);
        builder.setTitle(mOrder.getOrderId()+"");
        builder.setMessage("删除操作不可逆！！确认删除吗？");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	
            	String url=Constan.BASE_URL+"OrderDeleteConsumerServlet";
            	new HttpClientPostThread(url, mHandler, mOrder.getOrderId()).start();;
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	
            }
        });
        builder.show();
	}
	public void IbBack(View v) {

		OrderHistoryDetailConsumerActivity.this.finish();
	}
}
